<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/2/26
 * Time: 15:38
 */

namespace Home\Controller;


use Home\Model\ActModel;
use Home\Model\MesModel;
use Home\Model\TypeModel;
use Home\Model\VipModel;

class VipController extends CommonController
{
    public function index()
    {
        $data =i('post.ans');
        if(IS_POST){
            if(trim(empty($data))){
                $this->error('请输入查询的内容');
            }
            $data = (new VipModel())->where(['vip_name|vip_photo'=>$data])->select();
            foreach ($data as $k=>$v){
                $data[$k]['vip_pid'] = (new TypeModel())->where(['cid'=>$v['vip_pid']])->field('typename')->find();
            }
            if(!$data){
                $this->error('没有查询到此条信息，请检查输入');
            }
            $this->assign('list',$data);
        }
            $this->display();

    }


    public function add()
    {
        if(IS_POST){
            $data = i('post.');
            if(trim(empty($data['name']))){
                $this->error('请输入会员名称');
            }
            if(trim(empty($data['photo']))){
                $this->error('请输入会员手机号');
            }
            if(strlen($data['photo'])!=11){
                $this->error('请输入正确的手机号格式，长度不小于或大于11位。');
            }
            $date = [
                'vip_name'=>$data['name'],
                'vip_time'=>time(),
                'vip_photo'=>$data['photo'],
                'vip_pid'=>$data['pid'],
                'vip_money'=>$data['money'],
            ];
            $res = (new VipModel())->add($date);
            if($res){
                (new ActModel())->act('新增会员 '.$data['name']);
                $this->success('操作成功',u('Vip/group'));
            }else{
                $this->error('操作失败，请稍后重试');
            }
        }else {
            $dog = (new TypeModel())->where(['pid'=>1])->select();
            $this->assign('dog',$dog);
            $this->display();
        }
    }
    public function group()
    {
        $user = (new VipModel());

        $count      = $user->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,9);// 实例化分页类 传入总记录数和每页显示的记录数(25)

        $Page->setConfig('theme',"%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show       = $Page->show();// 分页显示输出

        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $user->order('vip_time desc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $dog = (new TypeModel())->where(['pid'=>1])->field('typename')->select();

        foreach ($list as $k => $v){
            $list[$k]['vip_pid'] = (new TypeModel())->where(['cid'=>$v['vip_pid']])->field('typename')->find();
        }
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    public function del()
    {
        $id = i('get.id');
        $user = (new VipModel())->where(['vip_id'=>$id])->find();
        $res = (new VipModel())->where(['vip_id'=>$id])->delete();
        if($res){
            (new ActModel())->act('删除会员 '.$user['vip_name']);
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

    public function edit()
    {
        $id = i('get.id');
        if(IS_POST){
            $data = i('post.');
            if(trim(empty($data['name']))){
                $this->error('请输入会员名称');
            }
            if(trim(empty($data['photo']))){
                $this->error('请输入会员手机号');
            }
            if(strlen($data['photo'])!=11){
                $this->error('请输入正确的手机号格式，长度不小于或大于11位。');
            }
            $date =[
                'vip_name'=>$data['name'],
                'vip_photo'=>$data['photo'],
                'vip_pid'=>$data['pid'],
            ];
            $res = (new VipModel())->where(['vip_id'=>$id])->save($date);
            if($res){
                $this->success('修改成功',u('Vip/group'));
            }else{
                $this->error('修改失败，请稍后重试');
            }
        }else{
            $user = (new VipModel())->where(['vip_id'=>$id])->find();
            $dog = (new TypeModel())->where(['pid'=>1])->select();
            $this->assign('vo',$user);
            $this->assign('dog',$dog);
            $this->display();
        }
    }

    public function addmoney()
    {
        $id = i('get.id');
        $data = i('post.');
        if(IS_POST){
            if(trim(empty($data['money']))){
                $this->error('充值金额不能为空');
            }
            $usermoney  = (new VipModel())->where(['vip_id'=>$id])->find();
            $date = [
                'vip_money'=>$data['money']+$usermoney['vip_money'],
            ];
            $res = (new VipModel())->where(['vip_id'=>$id])->save($date);
            if($res){
                $data = [
                    'mes_time'=>time(),
                    'mes_message'=>'会员充值收入'.i('post.money').'元',
                ];
                (new MesModel())->add($data);
                (new ActModel())->act('会员 '.$usermoney['vip_name'].' 充值 '.i('post.money').' 元');
                $this->success('充值成功',u('Vip/group'));
            }else{
                $this->error('充值失败，请稍后重试');
            }
        }else {
            $this->display();
        }
    }

    public function resmoney()
    {
        $id = i('get.id');
        $data = i('post.');
        if(IS_POST){
            if(trim(empty($data['obj']))){
                $this->error('请填写消费事项');
            }
            if(trim(empty($data['money']))){
                $this->error('请输入消费金额');
            }
            $user = (new VipModel())->where(['vip_id'=>$id])->find();
            if(strlen($data['seal'])>2){
                $this->error('请不要输入大于两位数的数值。');
            }
            if(strlen($data['seal'])==2){
                $data['seal']=$data['seal']/10;
            }
            $seal = $data['money']*($data['seal']/10);
            $end = $user['vip_money']-$seal;
            if($end<30){
                $this->error("会员 {$user['vip_name']} 账户余额不足30元，请充值");
            }
            $date = [
                'vip_money'=>$end,
            ];
            $res = (new VipModel())->where(['vip_id'=>$id])->save($date);
            if($res){
                (new ActModel())->act('会员 '.$user['vip_name'].' 宠物 '.i('post.obj').' 消费 '.$seal.' 元');
                $this->success('消费成功',u('Vip/group'));
            }else{
                $this->error('消费失败，请稍后重试');
            }
        }else {
            $resmoney = (new TypeModel())->where(['pid'=>8])->select();
            $this->assign('resmoeny',$resmoney);
            $this->display();
        }
    }
}